﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.DataAccess.Project {
    public class MouldShiftRepository : BaseRepository<MouldShift> {

        private int _pageSize;

        public MouldShiftRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public MouldShift FetchMouldShiftById(int mouldShiftId) {
            using (var ctx = this.Context) {
                return ctx.MouldShifts.FirstOrDefault(x => x.MouldShiftId == mouldShiftId);
            }
        }        
        
        public void UpdateMouldShift(MouldShift mouldShift) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        Mould mould = ctx.Moulds.Find(mouldShift.MouldId);
                        mould.VendorId = mouldShift.ToVendorId;
                        // update last shifting date
                        ctx.Moulds.Attach(mould);
                        ctx.Entry(mould).State = EntityState.Modified;

                        ctx.MouldShifts.Attach(mouldShift);
                        ctx.Entry(mouldShift).State = EntityState.Modified;
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public void DeleteMultipleMouldShift(params int[] mouldShiftIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.MouldShifts.Where(x => mouldShiftIds.Contains(x.MouldShiftId)).ToList().ForEach(x => {
                            ctx.MouldShifts.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public List<MouldShift> FetchMouldShiftList(MouldShiftSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.MouldShifts
                    .Include("Mould")
                    .Include("Vendor")
                    .Include("Vendor1")
                    .Where(x => !string.IsNullOrEmpty(x.Code));

                if (!string.IsNullOrEmpty(searchModel.Code)) {
                    query = query.Where(x => x.Code.Contains(searchModel.Code));
                }
                if (searchModel.MouldId.HasValue) {
                    query = query.Where(x => x.MouldId == searchModel.MouldId.Value);
                }
                if (searchModel.FromVendorId.HasValue) {
                    query = query.Where(x => x.FromVendorId == searchModel.FromVendorId.Value);
                }
                if (searchModel.ToVendorId.HasValue) {
                    query = query.Where(x => x.ToVendorId == searchModel.ToVendorId.Value);
                }
                if (searchModel.ShiftDateStart.HasValue) {
                    query = query.Where(x => x.ShiftDate >= DbFunctions.TruncateTime(searchModel.ShiftDateStart.Value));
                }
                if (searchModel.ShiftDateEnd.HasValue) {
                    query = query.Where(x => x.ShiftDate <= DbFunctions.TruncateTime(searchModel.ShiftDateEnd.Value));
                }

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderByDescending(x => x.CreatedDatetime).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }
    }
}
